package com.ayoka.parenting.ui.meditation.question;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.ViewGroup;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.MeditationQuestionBean;
import com.ayoka.parenting.bean.ResponseMeditationQuestion;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.mvp.BasePresenterImpl;
import com.jincp.common.dialog.DialogDismissListener;
import com.jincp.common.dialog.DialogResultListener;
import com.jincp.common.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: getMeditationQuestionPresenter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MeditationQuestionPresenter extends BasePresenterImpl<MeditationQuestionContract.View> implements MeditationQuestionContract.Presenter {

    private MeditationQuestionModel model;

    public MeditationQuestionPresenter() {
        model = new MeditationQuestionModel();
    }

    @Override
    public void getMeditationQuestionData(String recordId) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.getMeditationQuestionPresenter(this, recordId, new SimpleRequestCallback<ResponseMeditationQuestion>() {
            @Override
            public void onSuccess(ResponseMeditationQuestion data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().configMeditationQuestionData(data);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
            }
        });
    }

    @Override
    public void saveQuestionAnswer(String recordId, ResponseMeditationQuestion questionBean) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        List<MeditationQuestionBean.AnswerResultBean> answerResultList = formatAnswerResultList(questionBean);
        model.saveQuestionAnswer(this, recordId, answerResultList, new SimpleRequestCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onSaveQuestionAnswerCallback(true);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onSaveQuestionAnswerCallback(false);
            }
        });
    }

    @Override
    public void showSynchronizedCircleGroupDialog(FragmentManager fragmentManager, ResponseMeditationQuestion questionBean) {
        final String content = getContent(questionBean);
        SynchronizedCircleGroupDialog dialog = new SynchronizedCircleGroupDialog.Builder()
                .setGravity(Gravity.BOTTOM)
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setMessage(content)
                .build();
        dialog.setDialogResultListener(new DialogResultListener() {
            @Override
            public void result(DialogFragment dialogFragment, Object result) {
                dialogFragment.dismissAllowingStateLoss();
                if ((boolean) result) {
                    synchronizedCircleGroup(content);
                } else {
                    if (!isAttached()) return;
                    mView.get().onSynchronizedCircleGroupCallback(false);
                }
            }
        });
        dialog.show(fragmentManager, "SynchronizedCircleGroupDialog");
    }

    private void synchronizedCircleGroup(String content) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.synchronizedCircleGroup(this, content, new SimpleRequestCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onSynchronizedCircleGroupCallback(true);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onSynchronizedCircleGroupCallback(false);
            }
        });
    }


    @Override
    public void showMeditationCompletedDialog(FragmentManager fragmentManager, int dayNumber) {
        MeditationCompletedDialog dialog = new MeditationCompletedDialog.Builder()
                .setGravity(Gravity.BOTTOM)
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
                .setDayNumber(dayNumber)
                .build();
        dialog.setDialogDismissListener(new DialogDismissListener() {
            @Override
            public void dismiss(DialogFragment dialog) {
                if (!isAttached()) return;
                mView.get().onDialogDismiss(dialog);
            }
        });
        dialog.show(fragmentManager, "MeditationCompletedDialog");
    }

    private List<MeditationQuestionBean.AnswerResultBean> formatAnswerResultList(ResponseMeditationQuestion questionBean) {
        List<MeditationQuestionBean.AnswerResultBean> result = new ArrayList<>();
        for (MeditationQuestionBean question : questionBean.questions) {
            result.add(question.Answer);
        }
        return result;
    }

    public String getContent(ResponseMeditationQuestion responseMeditationQuestion) {
        StringBuilder stringBuilder = new StringBuilder();
        if (responseMeditationQuestion != null && responseMeditationQuestion.meditation != null) {
            stringBuilder.append("#");
            stringBuilder.append(responseMeditationQuestion.meditation.GroupTitle);
            stringBuilder.append(" ");
            stringBuilder.append(responseMeditationQuestion.meditation.Title);
            stringBuilder.append("\n\n");
            if (responseMeditationQuestion.questions != null) {
                MeditationQuestionBean questionBean = responseMeditationQuestion.questions.get(responseMeditationQuestion.questions.size() - 1);
                if (questionBean != null && questionBean.Answer != null) {
                    stringBuilder.append(questionBean.Answer.Answer);
                }
            }
        }
        return stringBuilder.toString();
    }
}
